/* ===== selfplayingmethodlist =====：自营列表 优惠券*/
myApp.onPageInit('selfplayingmethodlist', function (page) {
	console.log("selfplayingmethodlist");
    var selfplayingmethod_city = global_cityid;
    
    //下拉加载的参数初始化
    // Loading flag
	var loading = false;
    // Append items per load
	var selfplaymenthoditemsPerLoad = selfplaymenthodpageitem;
	
	//city
    $$.getJSON(url_get_city, {}, function (ajaxresult) {
        if(ajaxresult.code==1){
            var data_selfplayingmethodcity = ajaxresult.data;
            var html_selfplayingmethodcity = myApp.playingMethodCityTemplate(data_selfplayingmethodcity);
            $$('div.selfplayingmethodcity ul').html(html_selfplayingmethodcity);
        }else{
            ajaxfail(ajaxresult.msg);
        }
        //选定全局城市
        var activedom = ".selfplayingmethodcity a.btn_item[data-id='" + selfplayingmethod_city.toString() + "']";
        $$(activedom).addClass('active');
        //更新标题
        var city = '';
        city=$$(activedom).html(); 
        $$('.selfplayingmethodcity_title span').html(city);
        
        //点击city
        $$('.selfplayingmethodcity a.btn_item').click(function(){
            $$(this).toggleClass('active');
        });

    });
    
    $$('.filter_submit').click(function(){
    	var city='';
        //更新标题
        if($$('.selfplayingmethodcity a.active').length>1){
			$$('.selfplayingmethodcity a.active').each(function(){
				city+=$$(this).data('city')+',';
			});
		}else if($$('.selfplayingmethodcity a.active').length==1){
			city=$$('.selfplayingmethodcity a.active').data('city');
		}
        if(!city){city='城市'}
        $$('.selfplayingmethodcity_title span').html(city);
        //刷新页面
        getselfplaymethodlist(0,selfplaymenthodpageitem,'refresh');        
    });
   
	//type
    $$.getJSON(url_get_selfplayingmethodtype, {}, function (ajaxresult) {
        console.log(ajaxresult);
        if(ajaxresult.code==1){                   
            var data_playingmethodtype = ajaxresult.data;
            var html_playingmethodtype = myApp.playingmethodtypeTemplate(data_playingmethodtype);
            $$('div.selfplayingmethodtype ul').html(html_playingmethodtype);
            //type click
            $$('.selfplayingmethodtype a.btn_item').click(function(){
                $$('.selfplayingmethodtype a.btn_item').removeClass('active');
				$$(this).addClass('active');
                //更新标题
                var type = '';
                if($$(this).data('id')=='0'){
                	type='类型';
                }else{type=$$(this).html();} 
                $$('.selfplayingmethodtype_title span').html(type);
                //刷新页面
                getselfplaymethodlist(0,selfplaymenthodpageitem,'refresh');
            });
        }else{
            ajaxfail(ajaxresult.msg);
        }
    });
    
    //排序click
    $$('.selfplayingmethodsort a.btn_item').click(function(){
    	$$('.selfplayingmethodsort a.btn_item').removeClass('active');
		$$(this).addClass('active');
    	//更新标题
    	var sort = '';
        if($$(this).data('id')=='0'){
        	sort='排序';
        }else{sort=$$(this).html();}     
        $$('.selfplayingmethodsort_title span').html(sort);
    	//刷新页面
    	getselfplaymethodlist(0,selfplaymenthodpageitem,'refresh');
    });

    //init list
	$$.getJSON(url_get_selfplayingmethodlist, {cityId:selfplayingmethod_city,limit:selfplaymenthodpageitem,offset:0}, function (ajaxresult) {
		console.log(ajaxresult);
		if(ajaxresult.code==1){
			var data_selfplayingmethodlist = ajaxresult.data;
			var html_selfplayingmethodlist = myApp.playSelfItemTemplate(data_selfplayingmethodlist);
    		$$('#selfplayingmethodlist .div_selfplayingmethodcontent').html(html_selfplayingmethodlist);
    		
    		//页面第一次加载
    		// Last loaded index
    		var lastIndex=$$('.div_selfplayingmethodcontent .self_playitem').length;
    		$$("input[name='selfplayingmethod_lastIndex']").val(lastIndex);
		}else{
			ajaxfail(ajaxresult.msg);
		}
        
	});
	
	//滚动加载
    // 注册'infinite'事件处理函数
    $$('#selfplayingmethodlist .infinite-scroll').on('infinite', function () {
     // 如果正在加载，则退出
     if (loading) return;
     // 设置flag
     loading = true;
     // 模拟1s的加载过程
     setTimeout(function () {
        // 重置加载flag
        loading = false;
        // last load index
		var lastIndex = $$("input[name='selfplayingmethod_lastIndex']").val();
		// Generate new items HTML and Update last loaded index
		getselfplaymethodlist(lastIndex,selfplaymenthoditemsPerLoad,'append'); 
      }, 1000);
    });
    
    //subnav的收放
	$$(".selfplayingmethodcity_title").click(function(){
		var stop=false;
		//关闭其他
    	if ($$('.selfplayingmethodtype').hasClass('grade-w-roll')){
            $$('.selfplayingmethodtype').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.selfplayingmethodsort').hasClass('grade-w-roll')){
            $$('.selfplayingmethodsort').removeClass('grade-w-roll');
            stop=true;
        };
        //打开关闭自身
        if ($$('.selfplayingmethodcity').hasClass('grade-w-roll')) {
            $$('.selfplayingmethodcity').removeClass('grade-w-roll');
            $$("#selfplayingmethodlist").removeClass('with-shadow');
        }else {
        	if(stop){
        		setTimeout("$$('.selfplayingmethodcity').addClass('grade-w-roll')",500);
        	}else{
        		$$("#selfplayingmethodlist").addClass('with-shadow');
                $$('.selfplayingmethodcity').addClass('grade-w-roll');
            }
        }        
    });
    
    $$(".selfplayingmethodtype_title").click(function(){
    	var stop=false;
    	if ($$('.selfplayingmethodcity').hasClass('grade-w-roll')){
            $$('.selfplayingmethodcity').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.selfplayingmethodsort').hasClass('grade-w-roll')){
            $$('.selfplayingmethodsort').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.selfplayingmethodtype').hasClass('grade-w-roll')) {
            $$('.selfplayingmethodtype').removeClass('grade-w-roll');
            $$("#selfplayingmethodlist").removeClass('with-shadow');
        }else {
        	if(stop){
        		setTimeout("$$('.selfplayingmethodtype').addClass('grade-w-roll')",500);
        	}else{
        		$$("#selfplayingmethodlist").addClass('with-shadow');
                $$('.selfplayingmethodtype').addClass('grade-w-roll');
            }
        }        
    });
    
    $$(".selfplayingmethodsort_title").click(function(){
    	var stop=false;
    	if ($$('.selfplayingmethodtype').hasClass('grade-w-roll')){
            $$('.selfplayingmethodtype').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.selfplayingmethodcity').hasClass('grade-w-roll')){
            $$('.selfplayingmethodcity').removeClass('grade-w-roll');
            stop=true;
        };

        if ($$('.selfplayingmethodsort').hasClass('grade-w-roll')) {
            $$('.selfplayingmethodsort').removeClass('grade-w-roll');
            $$("#selfplayingmethodlist").removeClass('with-shadow');
        }else {
        	if(stop){
        		setTimeout("$$('.selfplayingmethodsort').addClass('grade-w-roll')",500);
        	}else{
        		$$("#selfplayingmethodlist").addClass('with-shadow');
                $$('.selfplayingmethodsort').addClass('grade-w-roll');
            }
        }        
    });    
});

/* ===== selfplayingmethoddetail =====：详情页 */
myApp.onPageInit('selfplayingmethoddetail', function (page) {
	console.log('selfplayingmethoddetail');
	var selfplayid=$$("#selfplayingmethoddetail").data("id");
	
	var selfplaydetailswiper = new Swiper('.swiper-selfplaydetail', {
		pagination:".swiper-pagination-selfplaydetail",
		loop:true,
	    autoplay:5000,
	    autoplayDisableOnInteraction:false
	});
	
	$$.getJSON(url_get_selfplaydetaildesc, {id:selfplayid}, function (ajaxresult) {
		if(ajaxresult.code==1){
			var data_selfplayingmethoddesc = ajaxresult.data;
			var html_selfplaydescline = myApp.selfPlayDescLineTemplate(data_selfplayingmethoddesc);
			var html_selfplaydescprice = myApp.selfPlayDescPriceTemplate(data_selfplayingmethoddesc);
    		$$('#desc ul').html(html_selfplaydescline);
    		$$('#price ul').html(html_selfplaydescprice);
    		
    		//加载完毕显示
    		$$('div.selfdesc').removeClass("hidden");
    		$$('div.loader').addClass("hidden");
    		
    		//导航点击
    	    $$("#selfplayingmethoddetail .buttons-row .button").click(function(){
    	    	$$("#selfplayingmethoddetail .buttons-row .button").removeClass('active');
    	    	$$(this).addClass("active");
    	    	var targetId="#"+$$(this).data('id');
    	    	var targetY=$$(targetId).offset().top+$$("#selfplayingmethoddetail .page-content").scrollTop()-88;
    	    	$$("#selfplayingmethoddetail .page-content").scrollTop(targetY);
    	    });
    	    $$("#selfplaymethoddetail-subnav .buttons-row .button").click(function(){
    	    	$$("#selfplaymethoddetail-subnav .buttons-row .button").removeClass('active');
    	    	$$(this).addClass("active");
    	    	var targetId="#"+$$(this).data('id');
    	    	var targetY=$$(targetId).offset().top+$$("#selfplayingmethoddetail .page-content").scrollTop()-88;
    	    	$$("#selfplayingmethoddetail .page-content").scrollTop(targetY);
    	    });
    	    
    	    var buttonrowY=$$("#selfplayingmethoddetail .buttons-row").offset().top-44;
    	    //navbar和subnavbar高88px+2px
    	    var descY=$$("#desc").offset().top-90;
    	    
    	    var descimgnum=$$("#desc .list-block .item-content").length;
    	    var descW=$$("#desc").width();
    	    var descimgH=descW*1.415*descimgnum;
    	    //#price .card-footer高45px, #desc和#price间隔10px 
    	    var priceY=descY+45+descimgH+10;
    	    
    	    var priceimgnum=$$("#price .list-block .item-content").length;
    	    var priceW=$$("#price").width();
    	    var priceimgH=priceW*1.415*priceimgnum;
    	    //#price .card-footer高45px, #price和#remark间隔10px
    	    var remarkY=priceY+45+priceimgH+10;
            //导航滚动
    	    $$("#selfplayingmethoddetail .page-content").scroll(function(){
    	    	var top=$$(this).scrollTop();
    	    	//固定在顶部
    	    	if(top>buttonrowY){
    	    		if(!$$("#selfplayingmethoddetail .buttons-row").hasClass('hidden')){
    	    			$$("#selfplayingmethoddetail .buttons-row").addClass('hidden');
    	    		}    		
    	    		if($$("#selfplaymethoddetail-subnav").hasClass('hidden')){
    	    			$$("#selfplaymethoddetail-subnav").removeClass('hidden');
    	    		}
    	    	}else{
    	    		if($$("#selfplayingmethoddetail .buttons-row").hasClass('hidden')){
    	    			$$("#selfplayingmethoddetail .buttons-row").removeClass('hidden');
    	    		}    		
    	    		if(!$$("#selfplaymethoddetail-subnav").hasClass('hidden')){
    	    			$$("#selfplaymethoddetail-subnav").addClass('hidden');
    	    		}
    	    	}
    	    	//button active
    	    	if(top<priceY){
    	    		$$("#selfplayingmethoddetail .buttons-row .button").removeClass('active');
    	        	$$("#selfplayingmethoddetail .buttons-row a[data-id='desc']").addClass("active");   		
    	        	$$("#selfplaymethoddetail-subnav .buttons-row .button").removeClass('active');
    	        	$$("#selfplaymethoddetail-subnav .buttons-row a[data-id='desc']").addClass("active");
    	    	}    	    	
    	    	else if(top>priceY && top<remarkY){
    	    		$$("#selfplayingmethoddetail .buttons-row .button").removeClass('active');
    	        	$$("#selfplayingmethoddetail .buttons-row a[data-id='price']").addClass("active");   		
    	        	$$("#selfplaymethoddetail-subnav .buttons-row .button").removeClass('active');
    	        	$$("#selfplaymethoddetail-subnav .buttons-row a[data-id='price']").addClass("active");
    	    	}
    	    	else if(top>remarkY){
    	    		$$("#selfplayingmethoddetail .buttons-row .button").removeClass('active');
    	        	$$("#selfplayingmethoddetail .buttons-row a[data-id='remark']").addClass("active");   		
    	        	$$("#selfplaymethoddetail-subnav .buttons-row .button").removeClass('active');
    	        	$$("#selfplaymethoddetail-subnav .buttons-row a[data-id='remark']").addClass("active");
    	    	}
    	    });
		}else{
			ajaxfail(ajaxresult.msg);
		}
        
	});
	
    $$('#selfplayingmethoddetail .msg').on('click',function(){
        var rev_id = $$(this).data('id');
        $$.getJSON(url_get_chat, {
            rev_id:rev_id,// 接收对象id
            page:1,
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                console.log(ajaxresult.data);
                mainView.router.load({
                    "url":"msg.html",
                    "context":ajaxresult.data
                }); 
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
    $$('#selfplayingmethoddetail .order').on('click',function(){
        var id = $$(this).data('id');
        var type = $$(this).data('type');
        console.log(id,type);
        $$.getJSON(url_get_getbookinginfo, {
            goodsId:id,// 接收对象id
            type:type,
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                console.log(ajaxresult.data);
                mainView.router.load({
                    "url":"order.html",
                    "context":ajaxresult.data
                }); 
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
    $$('#selfplayingmethoddetail .remark').on('click',function(){
        var id = $$(this).data('id');
        mainView.router.load({
            "url":"remark.html",
            "context":{"id":id}
        }); 
    });
});
